package com.share.main;

import com.alibaba.fastjson.JSON;
import com.share.domain.User;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.ClusterClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;

/**
 * @author caifan
 * 2018/6/24
 */
@Log4j2
public class RestIndexApi {
    private String[] cardNo = {"440101199103014278", "440101199103017997", "440101199103016214",
            "440101199103011376","440101199103018615", "310115198105012299",
            "310115198105011552", "310115198105019810", "310115198105019933", "310115198105019917"
            };
    RestHighLevelClient client;
    @Before
    public void init() {
         client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("192.168.5.186", 9200,"http"),
                new HttpHost("192.168.5.186", 9201, "http"),
                new HttpHost("192.168.5.186", 9202, "http")
        ).setMaxRetryTimeoutMillis(60000));
    }

    @Test
    public void addIndexTest() throws IOException {
        ClusterClient cluster = client.cluster();


        /**
         * 添加json字符串
         */
        /*String jsonString = "{" +
                "\"user\":\"share\"," +
                "\"postDate\":\"2018-03-30\"," +
                "\"message\":\" Elasticsearch test\"" +
                "}";
        request.source(jsonString, XContentType.JSON);
       client.index(request);

        List<User> userList = new ArrayList<>();
        for (int i=0;i<9;i++) {
            User user = new User();
            user.setAge(30 + i);
            user.setCardNo(cardNo[i]);
            user.setCreateTime(new Date());
            user.setEmail("lisi" + i + "@126.com");
            user.setUsername("lisi" + i);
            user.setNickname("haha" + i);
            request.source(JSON.toJSONString(user), XContentType.JSON);
            client.index(request);
        }*/

        BulkRequest bulkRequest = new BulkRequest();
        for (int i=0;i<9;i++) {
            IndexRequest request = new IndexRequest(
                    "posts2",
                    "tag");
            User user = new User();
            user.setAge(17 + i);
            user.setCardNo(cardNo[i]);
            user.setCreateTime(new Date());
            user.setEmail("zhaoliu" + i + "@126.com");
            user.setUsername("赵六" + i);
            user.setNickname("六儿" + i);
            request.source(JSON.toJSONString(user), XContentType.JSON);
            bulkRequest.add(request);
        }
        client.bulk(bulkRequest);

    }

    @Test
    public void updateRequestTest() throws Exception {
        UpdateRequest updateRequest = new UpdateRequest("posts2", "tag", "tBk2WmQBD3BOzhmTsjHH");
        User user = new User();
        user.setAge(17);
        user.setCreateTime(new Date());
        user.setUsername("赵六0");
        user.setNickname("六儿00");

        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
        client.update(updateRequest);
    }

    @Test
    public void existsTest() throws Exception {
        GetIndexRequest request = new GetIndexRequest();
        request.indices("post");
        log.info(client.indices().exists(request));
    }

    @Test
    public void deleteTest() throws Exception {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.type("tag");
        deleteRequest.index("posts2");
        deleteRequest.id("*");
        client.delete(deleteRequest);
    }

    @After
    public void close() throws IOException {
        if (null != client) {
            client.close();
        }
    }
}
